
#ifndef _SEFONIOT_COMMON_NET_H_
#define _SEFONIOT_COMMON_NET_H_

#include "SefonIotPlatform.h"


/**
 * @brief The structure of network connection(TCP or SSL).
 *   The user has to allocate memory for this structure.
 */

struct sfiot_network;
typedef struct sfiot_network sfiot_network_t, *sfiot_network_pt;

struct sfiot_network {
    const char *pHostAddress;
    uint16_t port;
    uint16_t ca_crt_len;

    /**< NULL, TCP connection; NOT NULL, SSL connection */
    const char *ca_crt;

    /**< connection handle: 0, NOT connection; NOT 0, handle of the connection */
    uintptr_t handle;

    /**< Read data from server function pointer. */
    int (*read)(sfiot_network_pt, char *, uint32_t, uint32_t);

    /**< Send data to server function pointer. */
    int (*write)(sfiot_network_pt, const char *, uint32_t, uint32_t);

    /**< Disconnect the network */
    int (*disconnect)(sfiot_network_pt);

    /**< Establish the network */
    int (*connect)(sfiot_network_pt);
};


int SFIotNetRead(sfiot_network_pt pNetwork, char *buffer, uint32_t len, uint32_t timeout_ms);
int SFIotNetWrite(sfiot_network_pt pNetwork, const char *buffer, uint32_t len, uint32_t timeout_ms);
int SFIotNetDisconnect(sfiot_network_pt pNetwork);
int SFIotNetConnect(sfiot_network_pt pNetwork);
int SFIotNetInit(sfiot_network_pt pNetwork, const char *host, uint16_t port, const char *ca_crt);

#endif /* SEFONIOT_COMMON_NET_H */
